﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using ProjectGamer.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace ProjectGamer.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedUsers=true, bool seedGames=true, bool seedRentals=true, bool seedAzGames=true)
        {
            List<ApplicationUser> UserList = new List<ApplicationUser>();

            var user01 = new ApplicationUser()
            {
                UserName = "user01",
                Email = "user01@mail.com"
            };
            var user02 = new ApplicationUser()
            {
                UserName = "user02",
                Email = "user02@mail.com"
            };
            var user03 = new ApplicationUser()
            {
                UserName = "user03",
                Email = "user03@mail.com"
            };
            var user04 = new ApplicationUser()
            {
                UserName = "user04",
                Email = "user04@mail.com"
            };

            UserList.Add(user01);
            UserList.Add(user02);
            UserList.Add(user03);
            UserList.Add(user04);

            if (seedUsers)
            {
                

                UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                foreach (var user in UserList)
                {
                    if (um.FindByName(user.UserName) == null)
                    {
                        var result = um.Create(user, "pass01");
                    }
                }
                db.SaveChanges();

            }

            user01 = db.Users.Where(x => x.UserName == user01.UserName).FirstOrDefault();
            user02 = db.Users.Where(x => x.UserName == user02.UserName).FirstOrDefault();
            user03 = db.Users.Where(x => x.UserName == user03.UserName).FirstOrDefault();
            user04 = db.Users.Where(x => x.UserName == user04.UserName).FirstOrDefault();

            if(seedGames)
            {
                db.Games.AddOrUpdate(
                    x => new {x.ApplicationUserId, x.Title},
                    new Game(user01.Id, "a1", "Call of Duty 4", "Shooter", "Xbox One", (float)45.50, (float)1.50, true, Condition.Good, false, false),
                    new Game(user02.Id, "a1", "Call of Duty 4", "Shooter", "Xbox One", (float)35.50, (float)1.00, true, Condition.Excellent, false, false),
                    new Game(user03.Id, "a2", "Halo 4", "Shooter", "Xbox 360", (float)0.00, (float)1.50, false, Condition.Good, false, false),
                    new Game(user04.Id, "a3", "Madden NFL 15", "Sports", "Playstation 4", (float)55.50, (float)2.00, false, Condition.Excellent, false, true),
                    new Game(user01.Id, "a3", "Madden NFL 15", "Sports", "Playstation 4", (float)55.50, (float)2.00, false, Condition.Excellent, false, false),
                    new Game(user02.Id, "a4", "Titanfall", "Shooter", "Xbox One", (float)54.50, (float)2.50, false, Condition.Excellent, false, false),
                    new Game(user03.Id, "a4", "Titanfall", "Shooter", "Xbox One", (float)54.50, (float)2.50, false, Condition.Excellent, false, false),
                    new Game(user04.Id, "a5", "Metro", "Shooter", "Playstation 4", (float)14.50, (float)0.00, true, Condition.Bad, false, false)
                    );
                db.SaveChanges();
            }

            if (seedAzGames)
            {
                db.AzGames.AddOrUpdate(
                    x => x.Id,
                    new AzGame("a1", "Call of Duty 4", "Shooter", "Xbox One", "http://ecx.images-amazon.com/images/I/51s2ZIykOyL.jpg"),
                    new AzGame("a2", "Halo 4", "Shooter", "Xbox 360", "http://ecx.images-amazon.com/images/I/91T4yYM0eGL._SL1500_.jpg"),
                    new AzGame("a3", "Madden NFL 15", "Sports", "Playstation 4", "http://ecx.images-amazon.com/images/I/61kzsnjB38L._SX385_.jpg"),
                    new AzGame("a4", "Titanfall", "Shooter", "Xbox One", "http://ecx.images-amazon.com/images/I/51rizlHJQ2L.jpg"),
                    new AzGame("a5", "Metro", "Shooter", "Playstation 4", "http://ecx.images-amazon.com/images/I/61NWVRw676L._SX385_.jpg")
                    );
                db.SaveChanges();
            }

            if (seedRentals)
            {
                db.Rentals.AddOrUpdate(
                    x => new { x.ApplicationUserId, x.GameId },
                    new Rental(user01.Id, 3, 0, (float)15.50, false, false),
                    new Rental(user02.Id, 4, (float)32.00, 0, false, false),
                    new Rental(user03.Id, 6, 0, 0, true, false),
                    new Rental(user04.Id, 8, 0, (float)1.00, false, true)
                    );
                db.SaveChanges();
            }

            
        }
    }
}
